﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ComeSeeThis_MVC2.Models
{
    public class SecurityRepository
    {
        // ReSharper disable InconsistentNaming
        private static readonly AspNetStuffEntities context = new AspNetStuffEntities();
        // ReSharper restore InconsistentNaming

        public Dictionary<aspnet_Operation, aspnet_Roles> OperationRole(Guid roleId)
        {
            //var boo = context.aspnet_Roles;

            var results = from b in context.aspnet_Roles
                          from c in b.aspnet_Operations
                          where b.RoleId == roleId
                          select new { b, c };

            return results.ToDictionary(result => result.c, result => result.b);
        }

        #region Operations

        #region Queries

        public IQueryable<aspnet_Operation> GetAllOperations()
        {
            return context.aspnet_Operation.OrderBy(x => x.Code);
        }
        public IQueryable<aspnet_Operation> GetAllOperationsByRoleName(string roleName)
        {
            return from x in context.aspnet_Operation
                   from y in x.aspnet_Roles
                   where y.LoweredRoleName == roleName.ToLower()
                   select x;
        }
        public IQueryable<aspnet_Operation> GetAllOperationsByRoleId(Guid id)
        {
            return from x in context.aspnet_Operation
                   from y in x.aspnet_Roles
                   where y.RoleId == id
                   select x;
        }
        public aspnet_Operation GetOperation(Guid operationId)
        {
            return context.aspnet_Operation.FirstOrDefault(x => x.OperationId == operationId);
        }
        public aspnet_Operation GetOperation(string code)
        {
            return context.aspnet_Operation.FirstOrDefault(x => x.Code == code);
        }

        #endregion

        #region Modify

        public void OperationAdd(aspnet_Operation aspnetOperation)
        {
            // Capitalize the code
            aspnetOperation.Code = aspnetOperation.Code.ToUpper();

            context.aspnet_Operation.AddObject(aspnetOperation);
        }
        public void OperationDelete(aspnet_Operation aspnetOperation)
        {
            context.aspnet_Operation.DeleteObject(aspnetOperation);
        }

        #endregion

        #endregion

        #region Roles

        #region Queries

        public aspnet_Roles GetRole(Guid roleId)
        {
            return context.aspnet_Roles.FirstOrDefault(x => x.RoleId == roleId);
        }
        public aspnet_Roles GetRole(string roleName)
        {
            return context.aspnet_Roles.FirstOrDefault(x => x.RoleName == roleName);
        }
        public IQueryable<aspnet_Roles> GetAllRoles()
        {
            return context.aspnet_Roles;
        }

        #endregion

        #region Modify

        public void RoleAdd(aspnet_Roles role)
        {
            context.aspnet_Roles.AddObject(role);
        }
        public void RoleDelete(aspnet_Roles role)
        {
            context.aspnet_Roles.DeleteObject(role);
        }

        #endregion

        #endregion

        #region Users

        public aspnet_Users GetUser(string userName)
        {
            return context.aspnet_Users.FirstOrDefault(x => x.UserName.ToLower() == userName.ToLower());
        }
        public aspnet_Users GetUser(Guid userId)
        {
            return context.aspnet_Users.FirstOrDefault(x => x.UserId == userId);
        }

        public static bool UserCanPerformOperation(Guid userId, string operationCode)
        {
            return context.vw_aspnet_UsersRolesOperations.Where(x=>x.UserId == userId).Where(x=>x.Code ==operationCode).Count() > 0;
            //return context.vw_aspnet_UsersOperations.Where(x => x.UserId == userId).Where(x => x.Code == operationCode).ToList().Count > 0;
        }
        public static bool UserCanPerformOperation(string userName, string operationCode)
        {
            var securityRepository = new SecurityRepository();
            var user = securityRepository.GetUser(userName);
            return UserCanPerformOperation(user.UserId, operationCode);
        }

        #endregion

        public void Save()
        {
            context.SaveChanges();
        }

    }
}